package cn.fsd.service;

import cn.fsd.domain.User;
import cn.fsd.exception.AppException;
import cn.fsd.exception.GateWayExceptionCodeMsg;
import cn.fsd.mapper.UserMapper;
import java.util.Objects;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

  @Autowired
  UserMapper userMapper;

  public User getUserByUserName(String userName) {
    return userMapper.selectByUsername(userName);
  }

  public boolean checkLogin(String userName, String password) {
    User user = userMapper.selectByUsername(userName);
    if(user==null){
      return false;
    }
    return Objects.equals(user.getPassword(),password);
  }

  public User addUser(User user){
    if(Objects.equals(user.getLevel(),1)){
      // TODO: 2023/7/10  若添加用户为客服中心则需要在操作员表里添加一条记录并拿到添加记录的id
      String operator_id = "123456";
      user.setOperator_id(operator_id);
    }
    if(Objects.equals(user.getLevel(),4)||Objects.equals(user.getLevel(),5)){
      //分站和库房需要指定所属的分站id或者库房id
      throw new AppException(GateWayExceptionCodeMsg.DEPT_ID_EMPTY);
    }
    user.setIs_super(0);
    user.setUser_id(UUID.randomUUID().toString());
    userMapper.insert(user);
    return user;
  }
}
